<template>
  <div id="questionSubmitView">
    <h2>题目列表</h2>
    <a-form :model="queryParams" layout="inline">
      <a-form-item field="questionId" label="题号">
        <a-input v-model="queryParams.questionId" placeholder="请输入题号" />
      </a-form-item>
      <a-form-item field="language" label="语言">
        <a-select
          v-model="queryParams.language"
          placeholder="请选择语言"
          style="min-width: 100px"
        >
          <a-option>java</a-option>
          <a-option>cpp</a-option>
          <a-option>go</a-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSubmit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0"></a-divider>
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: queryParams.pageSize,
        current: queryParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #judgeInfo="{ record }"
        >{{ JSON.stringify(record.judgeInfo) }}
      </template>
      <template #createTime="{ record }"
        >{{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { ref, watchEffect } from "vue";
import {
  QuestionControllerService,
  QuestionQueryRequest,
  QuestionSubmitQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { onMounted } from "vue";
import moment from "moment";

const dataList = ref([]);
const total = ref(0);
const queryParams = ref<QuestionSubmitQueryRequest>({
  questionId: undefined,
  language: undefined,
  pageSize: 10,
  current: 1,
});
const onPageChange = (current: number) => {
  queryParams.value = {
    ...queryParams.value,
    current: current,
  };
};
const doSubmit = () => {
  //这里需要重置搜索页号 否则在第X页数据太少时候  找不到第一页内容
  queryParams.value = {
    ...queryParams.value,
    current: 1,
  };
};
const loadData = async () => {
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    queryParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("获取失败" + res.message);
  }
};

onMounted(() => {
  loadData();
});
//监听queryParams变量改变
watchEffect(() => {
  loadData();
});

const router = useRouter();
const doQuestion = (question: any) => {
  router.push({
    path: `/view/question/${question.id}`,
  });
};

const columns = [
  {
    title: "提交号",
    dataIndex: "id",
  },
  {
    title: "编程语言",
    dataIndex: "language",
  },
  {
    title: "判题信息",
    slotName: "judgeInfo",
  },
  {
    title: "判题状态",
    dataIndex: "status",
  },
  {
    title: "题目id",
    dataIndex: "questionId",
  },
  {
    title: "提交者id",
    dataIndex: "userId",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
  },
];
</script>
<style scoped>
#questionSubmitView {
  padding: 16px;
}
</style>
